using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

namespace Project
{
    class IrrigationWindow:Window
    {
        private Window irrigationWindow;
        private Canvas canvas = new Canvas();
        private SimpleButton button_irrigation_on;
        private SimpleButton button_irrigation_off;
        public IrrigationWindow(Window mw)
        {
            irrigationWindow = new Window();
            irrigationWindow.Width = SystemMetrics.ScreenWidth;
            irrigationWindow.Height = SystemMetrics.ScreenHeight;

            CreateWindow();
        }

        public void CreateWindow()
        {
            Image image_irrigation_on = new Image(Resources.GetBitmap(Resources.BitmapResources.sprinkler_on));
            image_irrigation_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_irrigation_on.VerticalAlignment = VerticalAlignment.Center;
            button_irrigation_on = new SimpleButton(image_irrigation_on, irrigationWindow.Width, 4 * irrigationWindow.Height/5);
            button_irrigation_on.BackColor = Colors.White;

            Image image_irrigation_off = new Image(Resources.GetBitmap(Resources.BitmapResources.sprinkler_off));
            image_irrigation_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_irrigation_off.VerticalAlignment = VerticalAlignment.Center;
            button_irrigation_off = new SimpleButton(image_irrigation_off, irrigationWindow.Width, 4 * irrigationWindow.Height/5);
            button_irrigation_off.BackColor = Colors.White;
            
            if (Drivers.getInstance().led_irrigation.Read())
            {
                Canvas.SetTop(button_irrigation_on, 0);
                Canvas.SetLeft(button_irrigation_on, 0);
                canvas.Children.Add(button_irrigation_on);
            }
            else
            {
                Canvas.SetTop(button_irrigation_off, 0);
                Canvas.SetLeft(button_irrigation_off, 0);
                canvas.Children.Add(button_irrigation_off);
            }

            Image image_schedule = new Image(Resources.GetBitmap(Resources.BitmapResources.schedule));
            image_schedule.HorizontalAlignment = HorizontalAlignment.Center;
            image_schedule.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_schedule = new SimpleButton(image_schedule, irrigationWindow.Width / 2, irrigationWindow.Height / 5);
            button_schedule.BackColor = Colors.White;
            Canvas.SetBottom(button_schedule, 0);
            Canvas.SetLeft(button_schedule, 0);
            canvas.Children.Add(button_schedule);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, irrigationWindow.Width / 2, irrigationWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_schedule.Click += new EventHandler(buttonSchedule_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);
            button_irrigation_on.Click += new EventHandler(buttonIrrigationOn_Click);
            button_irrigation_off.Click += new EventHandler(buttonIrrigationOff_Click);
            
            
            irrigationWindow.Child = canvas;
            irrigationWindow.Visibility = Visibility.Visible;

        }

        void buttonIrrigationOn_Click(object sender, EventArgs evt)
        {            
            Drivers.getInstance().led_irrigation.Write(false);
            Debug.Print("Switching off irrigation");
            canvas.Children.Remove(button_irrigation_on);

            Canvas.SetTop(button_irrigation_off, 0);
            Canvas.SetRight(button_irrigation_off, 0);
            canvas.Children.Add(button_irrigation_off);
        }

        void buttonIrrigationOff_Click(object sender, EventArgs evt)
        {
            Drivers.getInstance().led_irrigation.Write(true);
            Debug.Print("Switching on irrigation");
            canvas.Children.Remove(button_irrigation_off);

            Canvas.SetTop(button_irrigation_on, 0);
            Canvas.SetRight(button_irrigation_on, 0);
            canvas.Children.Add(button_irrigation_on);
        }
        void buttonSchedule_Click(object sender, EventArgs evt)
        {
            IrrigationScheduleWindow lsw = new IrrigationScheduleWindow();
        }
        void buttonReturn_Click(object sender, EventArgs evt)
        {
            irrigationWindow.Close();
        }
    }
}
